package com.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.common.Result;
import com.tourism.entity.Menu;
import com.tourism.mapper.MenuMapper;
import com.tourism.service.MenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public Result getMenuById(Integer id) {
        if (id == null) {
            return Result.fail("菜单ID不能为空");
        }
        Menu menu = getById(id);
        return Result.success(menu);
    }

    @Override
    public Result getMenuList(Integer page, Integer pageSize, String name, String description) {
        // 创建分页对象
        Page<Menu> pageParam = new Page<>(page, pageSize);
        // 创建 LambdaQueryWrapper 用于构建查询条件
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();

        // 添加模糊查询条件
        if (name != null && !name.isEmpty()) {
            wrapper.like(Menu::getName, name);
        }
        if (description != null && !description.isEmpty()) {
            wrapper.like(Menu::getDescription, description);
        }

        // 执行分页查询
        IPage<Menu> pageResult = page(pageParam, wrapper);
        return Result.success(pageResult);
    }

    @Override
    public Result addMenu(Menu menu) {
        // 校验菜品名称
        if (menu.getName() == null || menu.getName().isEmpty()) {
            return Result.fail("菜品名称不能为空");
        }
        // 校验菜品价格
        if (menu.getPrice() == null || menu.getPrice().compareTo(java.math.BigDecimal.ZERO) <= 0) {
            return Result.fail("菜品价格必须大于0");
        }

        // 保存菜单信息
        boolean success = save(menu);
        return Result.success(success);
    }

    @Override
    public Result updateMenu(Menu menu) {
        // 校验菜单ID
        if (menu.getId() == null) {
            return Result.fail("菜单ID不能为空");
        }

        // 更新菜单信息
        boolean success = updateById(menu);
        return Result.success(success);
    }

    @Override
    public Result deleteMenu(Integer id) {
        // 校验菜单ID
        if (id == null) {
            return Result.fail("菜单ID不能为空");
        }

        // 删除菜单信息
        boolean success = removeById(id);
        return Result.success(success);
    }

    @Override
    @Transactional
    public Result batchDeleteMenus(List<Integer> ids) {
        // 校验ID列表
        if (ids == null || ids.isEmpty()) {
            return Result.fail("菜单ID列表不能为空");
        }

        // 批量删除菜单信息
        boolean success = removeBatchByIds(ids);
        return Result.success(success);
    }

    @Override
    public Result getAllMenus() {
        // 获取所有菜单信息
        List<Menu> menus = list();
        return Result.success(menus);
    }
}
